package poker;

import java.util.Random;
import java.util.ArrayList;
import java.util.Iterator;

import exception.InvalidCardEx;

public class Deck {
	private ArrayList<Card> d;
	private Iterator<Card> it;

	public Deck() throws InvalidCardEx {
		Random r = new Random();
		d = new ArrayList<Card>(52);
		int i;
		for (i = 0; i < 52; i++) {
			d.add(new Card(i));
		}

		Card buffer;
		buffer = new Card((short) 0);
		for (i = 51; i > 0; i--) {
			int j = r.nextInt(i + 1);
			buffer.setCard(d.get(j).getCard());
			d.get(j).setCard(d.get(i).getCard());
			d.get(i).setCard(buffer.getCard());
			it = d.iterator();
		}
	}

	public short takeCard() throws InvalidCardEx {
		if (it.hasNext()) {
			Card c = it.next();
			return c.getCard();
		} else {
			throw new InvalidCardEx();
		}
	}
}
